package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.umakr.ax.common.enm.AttentionFrom;
import com.umakr.ax.common.mapper.RadioStationFollowerMapper;
import com.umakr.ax.common.model.Attention;
import com.umakr.ax.common.model.RadioStationFollower;
import com.umakr.ax.core.base.BaseService;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 电台粉丝 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class RadioStationFollowerService extends BaseService<RadioStationFollowerMapper, RadioStationFollower>{

    @Autowired
    AttentionService attentionService;

    @Autowired
    Environment env;

    ThreadPoolExecutor cachedThreadPool = new ThreadPoolExecutor(20,500,1, TimeUnit.MINUTES,new LinkedBlockingDeque<Runnable>());

    /**
     * 关注 取消关注
     * @param userId 用户id
     * @param rsId  电台id
     * @param attention 关注或者取消
     * @param attentionFrom 来源
     * @return
     */
    public Object changeAttention(Long userId , Long rsId , Integer attention , Integer attentionFrom){
        int code = 0;
        String msg = "";

        Date date = new Date();

        if(attentionFrom == null && attention == 1) {
            msg = "关注来源不能为空";
        }else {
            if (attention >= 0 && attention <= 1) {

                EntityWrapper<RadioStationFollower> entityWrapper = new EntityWrapper<>();
                entityWrapper.eq("user_id", userId);
                entityWrapper.eq("rs_id", rsId);
                RadioStationFollower radioStationFollower = selectOne(entityWrapper);

                if (radioStationFollower == null) {
                    radioStationFollower = new RadioStationFollower();
                    radioStationFollower.setRsId(rsId);
                    radioStationFollower.setUserId(userId);
                    radioStationFollower.setAttentionFrom(attentionFrom);
                }

                if (attention == 0) {
                    delete(radioStationFollower.getId());
                    msg = "取消关注成功";
                } else if (attention == 1) {
                    radioStationFollower.setFollowTime(date);
                    update(radioStationFollower);
                    msg = "关注成功";
                }

                //插入关注记录表
                Attention att = new Attention();
                att.setRsId(rsId);
                att.setAttentionFrom(attentionFrom);
                att.setAttentionTime(date);
                att.setAttentionStatus(attention);
                attentionService.update(att);

            } else {
                msg = "编码错误";
            }
        }

        return resultMap(code,msg);
    }


    public Object delFollowers(String ids) {
        int errorCode = 0;
        String msg = "删除失败";
        Map<String,Object> data = Maps.newLinkedHashMap();
        if(!"".equals(ids)){
            String[] idsArr = ids.split(",");
            for (String id : idsArr){
                delFollower(Long.parseLong(id));
            }
            errorCode = 1;
            msg = "删除成功";
            data.put("result",1);
        }
        return resultMap(data,errorCode,msg);
    }

    public Object delFollower(Long id) {
        if(id != null){
            RadioStationFollower radioStationFollower = queryById(id);
            if(radioStationFollower != null){
                //插入关注记录表
                Attention attention = new Attention();
                attention.setRsId(radioStationFollower.getRsId());
                attention.setAttentionFrom(radioStationFollower.getAttentionFrom());
                attention.setAttentionTime(radioStationFollower.getFollowTime());
                attention.setAttentionStatus(0);
                attentionService.update(attention);
                //直接删除原来的关注数据
                delete(id);
            }
        }
        return true;
    }

    /**
     * 自动关注 接口
     * @param userId
     * @param rsId
     * @param attFrom
     */
    public Object autoAttention(Long userId,Long rsId,AttentionFrom attFrom){
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                EntityWrapper<RadioStationFollower> entityWrapper = new EntityWrapper<>();
                entityWrapper.eq("user_id", userId);
                entityWrapper.eq("rs_id", rsId);
                RadioStationFollower radioStationFollower = selectOne(entityWrapper);
                Date date = new Date();

                if(radioStationFollower == null){
                    radioStationFollower = new RadioStationFollower();
                    radioStationFollower.setRsId(rsId);
                    radioStationFollower.setUserId(userId);
                    radioStationFollower.setFollowTime(date);
                    radioStationFollower.setAttentionFrom(attFrom.ordinal());
                    update(radioStationFollower);
                }
                Attention attention = new Attention();
                attention.setRsId(radioStationFollower.getRsId());
                attention.setAttentionFrom(radioStationFollower.getAttentionFrom());
                attention.setAttentionTime(radioStationFollower.getFollowTime());
                attention.setAttentionTime(date);
                attention.setAttentionStatus(1);
                attentionService.update(attention);

            }
        });
        return resultMap(1,"");
    }
}
